package com.pbc.gmp.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.model.LoginUserInfo;
import com.pbc.gmp.model.OrgInfoVo;
import com.pbc.gmp.model.OrgTreeDto;
import com.pbc.gmp.model.OrgTypeRuleDto;
import com.pbc.gmp.model.basemodel.Org;
import com.pbc.gmp.model.basemodel.OrgType;
import com.pbc.gmp.model.basemodel.OrgTypeRule;
import com.pbc.gmp.service.IOrgService;
import com.pbc.gmp.service.IOrgTypeRuleService;
import com.pbc.gmp.service.IOrgTypeService;
import com.xiaoleilu.hutool.convert.Convert;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by UPC on 2017/12/9.
 */
@Controller
@RequestMapping("/org")
public class OrgAction extends BaseController {

    @Autowired
    private IOrgService orgService;
    @Autowired
    private IOrgTypeService orgTypeService;
    @Autowired private IOrgTypeRuleService orgTypeRuleService;

    /**
     * 进入组织架构管理页面
     *
     * @return
     */
    @RequestMapping(value="/manager")
    public String manager() {
        return "admin/organization/organization";
    }
    /**
     * 部门列表
     *
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public List<Org> treeGrid() {
        List<Org> ol=new ArrayList<Org>();
        ol.add(orgService.selectById(1));
        return ol;


    }
    @RequestMapping("/orgTree")
    @ResponseBody
    public List<OrgTreeDto> orgTree() {
       LoginUserInfo userInfo=(LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
//        List<OrgTreeDto> ol=new ArrayList<OrgTreeDto>();
//        ol.add(orgService.selectOrgTreeDtoById(Convert.toLong(1)));
        Long rootid=userInfo.getOrgTreeDtos().get(0).getId();
        List<OrgTreeDto> lists=orgService.selectOrgListByType(rootid,1);
        return  lists;


    }
    @RequestMapping("/allorg")
    @ResponseBody
    public List<OrgTreeDto> allOrg() {

        return orgService.selectAllOrg();


    }
    /**
     * 根据父ID得到子组织
     * @param pid
     * @return
     */
    @RequestMapping("/getchildlist")
    @ResponseBody
    public List<Org> getChildList(@RequestParam("id") Long pid) {

        return orgService.selectTreeGridByPid(pid) ;
    }
    /**
     * 根据父ID得到子组织
     * @param pid
     * @return
     */
    @RequestMapping("/getOrgTreeDtoChildList")
    @ResponseBody
    public List<OrgTreeDto> getOrgTreeDtoChildList(@RequestParam("id") Long pid) {

        return orgService.selectOrgTreeDtoByPid(pid);
    }
    /**
     * 进入组织编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editOrg")
    public String editOrg(Model model, @RequestParam Long id){
        Org organization = orgService.selectById(id);
        model.addAttribute("organization", organization);
        return "admin/organization/organizationEdit";
    }
    /**
     * 编辑部门
     *
     * @param organization
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Org organization) {
        orgService.updateById(organization);
        return renderSuccess("编辑成功！");
    }
    /**
     * 编辑部门详细列表
     *
     * @param
     * @return
     */
    @RequestMapping("/orglist")
    public Object orgList() {
        return "admin/organization/organizationList";
    }
    /**
     * 组织机构类型管理
     *
     * @param
     * @return
     */
    @RequestMapping("/typemanager")
    public Object orgTypePage() {


        return   "admin/organization/organizationTypeList";
    }

    /**
     * 查询组织机构类别列表
     * @return
     */
    @RequestMapping("/orgTypelist")
    @ResponseBody
    public Object orgTypeList() {
        return   orgTypeService.selectList(null);
    }

    @RequestMapping(value = "/addorgtype", method = {RequestMethod.POST })
    @ResponseBody
    public Object addOrgType(@RequestBody OrgType orgType){
        orgType.setId(Convert.toLong(0));
        orgTypeService.insert(orgType);
        return renderSuccess("添加成功！");
    }
    @RequestMapping(value = "/updateorgtype", method = {RequestMethod.POST })
    @ResponseBody
    public Object editOrgType(@RequestBody OrgType orgType){
        orgTypeService.updateById(orgType);
        return renderSuccess("编辑成功！");
    }
    @RequestMapping(value = "/deleteorgtype", method = {RequestMethod.POST })
    @ResponseBody
    public Object deleteOrgType(@RequestBody OrgType orgType){
        if(orgTypeService.deleteById(orgType.getId()))
        {
            return renderSuccess("删除成功！");
        }else {
            return renderSuccess("删除不成功！");
        }

    }

    @RequestMapping(value = "/orgTypeRuleEdit")
    @ResponseBody
    public List<OrgTypeRuleDto> orgTypeRuleEdit(@RequestParam Long  orgTypeId){


        return orgTypeService.selectOrgTypeRulelistBypid(orgTypeId);



    }
    @RequestMapping(value = "/editTypeRulePage",method = {RequestMethod.POST })

    public String orgTypeRuleEditManager(Model model, @RequestParam int id){
        model.addAttribute("typeId", id);
        return "admin/organization/organizationTypeRuleEdit";

    }
    @RequestMapping(value = "/addtyperule", method = {RequestMethod.POST })
    @ResponseBody
    public Object addorgTypeRule(@RequestBody OrgTypeRule orgTypeRule){
        orgTypeRule.setId(Convert.toLong(0));
        orgTypeRuleService.insert(orgTypeRule);
        return renderSuccess("添加成功！");

    }
    @RequestMapping(value = "/updatetyperule", method = {RequestMethod.POST })
    @ResponseBody
    public Object updateorgTypeRule(@RequestBody OrgTypeRule orgTypeRule){

        orgTypeRuleService.updateById(orgTypeRule);
        return renderSuccess("更新成功！");

    }
    @RequestMapping(value = "/deletetyperule")
    @ResponseBody
    public Object deleteorgTypeRule(@RequestBody Long id){

        orgTypeRuleService.deleteById(id);
        return renderSuccess("成功！");

    }
    @RequestMapping(value = "/getdepartmentlist", method = {RequestMethod.POST })
    @ResponseBody
    public List<OrgInfoVo> getDepartmentList(@RequestParam Long id){
//        LoginUserInfo userInfo= (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
//        List<OrgInfoVo> ov=orgService.getDepartmentListByUserId(Convert.toLong(userInfo.getId()));
        List<OrgInfoVo> orgInfoVos =orgService.selectDepartmentListByUserId(Convert.toLong(id));

        return orgInfoVos;

    }

}
